﻿using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Policy;
using System.Threading.Tasks;

namespace Zhaoxi.GrpcClient
{
	public class RedisHelper
	{

		public static string GetUrl()
		{
			string setid = "servicelist";
			string url = "";
			using (RedisClient client = new RedisClient("127.0.0.1", 6379))
			{

				var urls = client.GetAllItemsFromSortedSet(setid);
				var newusrls = client.GetAll<string>(urls.ToArray()).Where(m => m.Value != null).ToList();
				//实现负载均衡 // 随机的负载均衡 //可以自行发挥
				Random rnd = new Random();
				int rndNum = rnd.Next(newusrls.Count);
				url = newusrls[rndNum].Key;
				client.IncrementItemInSortedSet(setid, url, 1);
			}
			return url;
		}
	}
}
